
/*
Copyright 2009 Peter Ekler, Akos Ludanyi, Budapest University of Technology and Economics, Department of Automation and Applied Informatics, Applied Mobile Research Group (AMORG) http://amorg.aut.bme.hu

This file is part of MobTorrent.

MobTorrent is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

MobTorrent is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with MobTorrent.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ui;

import TorrentEngine.MTStringFormatter;
import TorrentEngine.MTTorrent;
import data.LocalizationSupport;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.StringItem;

/**
 *
 * @author Tyrael
 */
public class MTFormFileCreateState extends Form implements CommandListener
{
    public static final int ScreenID = 6;

    private MTUIManager uiManager;
    private Command command_Back;
    private StringItem stringItemCreateFile;
    private Gauge gaugeFileCreatePercent;

    public MTFormFileCreateState(String aTitle, MTUIManager aUIManager)
    {
        super(aTitle);
        uiManager = aUIManager;

        stringItemCreateFile = new StringItem(
            LocalizationSupport.getMessage("label_creatingFile")+":","");
        append(stringItemCreateFile);

        gaugeFileCreatePercent = new Gauge("", false, 100, 0);
        append(gaugeFileCreatePercent);

        command_Back = new Command(LocalizationSupport.getMessage("command_back"), Command.BACK, 0);
        addCommand(command_Back);
        setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d)
    {
        if (c == command_Back)
        {
            uiManager.showScreen(MTListTorrents.ScreenID);
        }
    }

    public void initialize(MTTorrent aTorrent)
    {
        setTitle(aTorrent.getTorrentNameWithoutPath());
    }

    public void startCreateNewFile(String aFileName)
    {
        stringItemCreateFile.setText(aFileName);
        gaugeFileCreatePercent.setLabel("0%");
        gaugeFileCreatePercent.setValue(0);
    }

    public void fileCreateStateChanged(String aFileName, double aPercent)
    {
        if (!stringItemCreateFile.getText().equals(aFileName))
        {
            stringItemCreateFile.setText(aFileName);
        }
        gaugeFileCreatePercent.setLabel(
            MTStringFormatter.getFormattedPercent(aPercent));
        gaugeFileCreatePercent.setValue((int)aPercent);
    }
}

